using System;
using System.Timers;
using CoreGraphics;
using Foundation;
using mb;
using mb.iOS;
using Masonry;
using MyExtensions;
using UIKit;

namespace libmp
{
    public class VCMemberCenterController : UIViewController
    {
        static ILog logger = mb.ServiceLocator.Instance.Get<ILog>();
        public VCMemberCenterController()
        {
            TabBarItem = new UITabBarItem("会员中心", null, 0);
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            CreateView();
        }
        private void CreateView()
        {
            var bkView = new UIView() { BackgroundColor = Util.MainBKColor };
            bkView.SetParent(View).MakeConstraints(m =>
            {
                m.Top.EqualTo(this.TopLayoutGuideBottom());
                m.Left.Right.Bottom.EqualTo(View);
            });
            var topBarView = createTopBar(View);
            var mainView = new MyScrollView();
            //var mainView = new UIView();
            mainView.BackgroundColor = UIColor.FromRGB(255, 255, 255);
            mainView.SetParent(View);
            mainView.MakeConstraints(m =>
            {
                m.Left.EqualTo(View);
                m.Right.EqualTo(View);
                m.Top.EqualTo(topBarView.Bottom());
                m.Bottom.EqualTo(this.BottomLayoutGuideTop());
            });
            createTopAdverise(mainView);
            createMiddleLine(mainView);
            createBuyMember(mainView);
            mainView.ContentSize = new CoreGraphics.CGSize(View.Bounds.Width, 765);
        }
        private UIView createTopBar(UIView View)
        {
            var btnPicWidth = 15;
            var btnPicHeight = 15;
            var topBarView = new UIView();
            topBarView.BackgroundColor = UIColor.FromRGB(251, 114, 153);
            topBarView.SetParent(View);
            topBarView.MakeConstraints(m =>
            {
                m.Left.EqualTo(View);
                m.Right.EqualTo(View);
                m.Top.EqualTo(this.TopLayoutGuideBottom());
                m.Height.EqualTo(NSObject.FromObject(48));
            });

            var topBarLeftView = new UIView();
            topBarLeftView.SetParent(topBarView);
            topBarLeftView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(10));
                m.Top.EqualTo(topBarView);
                m.Bottom.EqualTo(topBarView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width - 20) / 3));
            });

            var leftButton1 = new UIButton();
            leftButton1.Frame = new CGRect(0, 14, btnPicWidth + 30, btnPicHeight + 5);
            leftButton1.SetParent(topBarLeftView);
            var leftButton1ImageView = new UIImageView();
            var imgoperationArrow1 = UIImage.FromFile($"{App.HotStorageRoot}/res/user/Fanhui.png");
            leftButton1ImageView.Image = new UIImage(imgoperationArrow1.CGImage, 2, UIImageOrientation.Up);
            leftButton1ImageView.SetParent(leftButton1);
            leftButton1ImageView.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(leftButton1.CenterY());
                m.Left.EqualTo(NSObject.FromObject(5));
                m.Height.EqualTo(NSObject.FromObject(btnPicHeight));
                m.Width.EqualTo(NSObject.FromObject(btnPicWidth / 2));
            });
            var leftButton2Label = new UILabel();
            leftButton2Label.Text = "关闭";
            leftButton2Label.Font = UIFont.SystemFontOfSize(12);
            leftButton2Label.SetParent(leftButton1);
            leftButton2Label.TextColor = UIColor.White;
            leftButton2Label.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(leftButton1.CenterY());
                m.Left.EqualTo(NSObject.FromObject(20));
                m.Height.EqualTo(NSObject.FromObject(btnPicHeight));
                m.Width.EqualTo(NSObject.FromObject(btnPicWidth + 20));
            });
            leftButton1.TouchUpInside += (s, e) =>
            {
                logger.Log("关闭====>");
                AppDelegate.Instance.NavController.PopViewController(false);
            };

            var topBarMiddleView = new UIView();
            topBarMiddleView.SetParent(topBarView);
            topBarMiddleView.MakeConstraints(m =>
            {
                m.Left.EqualTo(topBarLeftView.Right());
                m.Top.EqualTo(topBarView);
                m.Bottom.EqualTo(topBarView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width - 20) / 3));
            });

            var middleLabel = new UILabel();
            middleLabel.Text = "会员中心";
            middleLabel.Font = UIFont.BoldSystemFontOfSize(18);
            middleLabel.SetParent(topBarMiddleView);
            middleLabel.TextColor = UIColor.White;
            middleLabel.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(topBarMiddleView.CenterY());
                m.CenterX.EqualTo(topBarMiddleView.CenterX());
            });

            var topBarRightView = new UIView();
            topBarRightView.SetParent(topBarView);
            topBarRightView.MakeConstraints(m =>
            {
                m.Left.EqualTo(topBarMiddleView.Right());
                m.Top.EqualTo(topBarView);
                m.Bottom.EqualTo(topBarView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width - 20) / 3));
            });
            return topBarView;
        }
        private void createTopAdverise(UIScrollView mainView)
        {
            var adveriseView = new UIView();
            adveriseView.SetParent(mainView);
            adveriseView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(15));
                m.Width.EqualTo(NSObject.FromObject(View.Bounds.Width - 30));
                m.Top.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(320));
            });
            var adveriseTitleView = new UIView();
            adveriseTitleView.SetParent(adveriseView);
            adveriseTitleView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Right.EqualTo(NSObject.FromObject(0));
                m.Top.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(40));
            });
            var title1 = new UILabel();
            title1.SetParent(adveriseTitleView);
            title1.Font = UIFont.BoldSystemFontOfSize(18);
            title1.TextColor = UIColor.FromRGB(33, 33, 33);
            title1.Text = "至尊会员：";
            title1.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(adveriseTitleView.CenterY());
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(24));
                m.Width.EqualTo(NSObject.FromObject(95));
            });
            var title2 = new UILabel();
            title2.SetParent(adveriseTitleView);
            title2.Font = UIFont.SystemFontOfSize(14);
            title2.Text = "独享4大特权，无限看电影！";
            title2.TextColor = UIColor.FromRGB(93, 93, 93);
            title2.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(adveriseTitleView.CenterY());
                m.Left.EqualTo(title1.Right());
                m.Right.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(24));
            });

            //广告图片
            var adverisePicView = new UIView();
            adverisePicView.SetParent(adveriseView);
            adverisePicView.BackgroundColor = UIColor.FromRGB(251, 114, 153);
            adverisePicView.Layer.CornerRadius = 10;
            adverisePicView.ClipsToBounds = true;
            adverisePicView.Layer.BorderWidth = 0;
            adverisePicView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Top.EqualTo(NSObject.FromObject(40));
                m.Height.EqualTo(NSObject.FromObject(140));
                m.Width.EqualTo(NSObject.FromObject(View.Bounds.Width - 30));
            });

            // var adImg = libmp.DataStore.store.onlineConfig.adImages[0];
            // Util.createAdverse(adImg, adverisePicView);

            var adveriserLabels = new string[4]
            {
                "无限播放",
                "无限缓存",
                "无广告",
                "独享服务器"
            };
            var adveriserLabelView = new UIView();
            adveriserLabelView.SetParent(adveriseView);
            adveriserLabelView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Right.EqualTo(NSObject.FromObject(0));
                m.Top.EqualTo(NSObject.FromObject(195));
                m.Height.EqualTo(NSObject.FromObject(120));
            });
            var labelWidth = (View.Bounds.Width - 30) / 2;
            var labelPix = 15;
            var labelPicWidth = labelWidth - (labelPix * 2);
            var labelHeight = 48;
            for (var i = 0; i < adveriserLabels.Length; i++)
            {
                var left = ((i % 2) * 2 + 1) * labelPix + (i % 2) * labelPicWidth;
                var top = Math.Floor((float) (i / 2)) * (labelHeight + 15);
                var labelView = new UIView();
                labelView.SetParent(adveriserLabelView);
                labelView.Layer.CornerRadius = 5;
                labelView.ClipsToBounds = true;
                labelView.Layer.BorderWidth = 0.2f;
                labelView.Layer.BorderColor = new CGColor(0.5f, 0.5f, 0.5f);
                labelView.MakeConstraints(m =>
                {
                    m.Left.EqualTo(NSObject.FromObject(left));
                    m.Top.EqualTo(NSObject.FromObject(top));
                    m.Width.EqualTo(NSObject.FromObject(labelPicWidth));
                    m.Height.EqualTo(NSObject.FromObject(labelHeight));
                });
                var labelText = new UILabel();
                labelText.SetParent(labelView);
                labelText.Text = adveriserLabels[i];
                labelText.TextColor = UIColor.FromRGB(66, 66, 66);
                labelText.TextAlignment = UITextAlignment.Center;
                labelText.MakeConstraints(m =>
                {
                    m.Left.EqualTo(NSObject.FromObject(0));
                    m.Top.EqualTo(NSObject.FromObject(0));
                    m.Width.EqualTo(NSObject.FromObject(labelPicWidth));
                    m.Height.EqualTo(NSObject.FromObject(labelHeight));
                });
            }
        }

        private void createMiddleLine(UIScrollView mainView)
        {
            var lineView = new UIView();
            lineView.SetParent(mainView);
            lineView.BackgroundColor = UIColor.FromRGB(248, 248, 248);
            lineView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Top.EqualTo(NSObject.FromObject(320));
                m.Height.EqualTo(NSObject.FromObject(10));
                m.Width.EqualTo(NSObject.FromObject(View.Bounds.Width));
            });
        }
        private void createBuyMember(UIScrollView mainView)
        {
            var adveriseView = new UIView();
            adveriseView.SetParent(mainView);
            adveriseView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(15));
                m.Top.EqualTo(NSObject.FromObject(335));
                m.Height.EqualTo(NSObject.FromObject(430));
                m.Width.EqualTo(NSObject.FromObject(View.Bounds.Width - 30));
            });
            var adveriseTitleView = new UIView();
            adveriseTitleView.SetParent(adveriseView);
            adveriseTitleView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Right.EqualTo(NSObject.FromObject(0));
                m.Top.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(40));
            });
            var title1 = new UILabel();
            title1.SetParent(adveriseTitleView);
            title1.Font = UIFont.BoldSystemFontOfSize(18);
            title1.TextColor = UIColor.FromRGB(33, 33, 33);
            title1.Text = "购买会员：";
            title1.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(adveriseTitleView.CenterY());
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(24));
                m.Width.EqualTo(NSObject.FromObject(95));
            });
            var title2 = new UILabel();
            title2.SetParent(adveriseTitleView);
            title2.Font = UIFont.SystemFontOfSize(14);
            title2.Text = "一次性购买独享超低折扣";
            title2.TextColor = UIColor.FromRGB(93, 93, 93);
            title2.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(adveriseTitleView.CenterY());
                m.Left.EqualTo(title1.Right());
                m.Right.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(24));
            });

            var buyInfoView = new UIView();
            buyInfoView.SetParent(adveriseView);
            buyInfoView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Top.EqualTo(NSObject.FromObject(50));
                m.Height.EqualTo(NSObject.FromObject(450));
                m.Width.EqualTo(NSObject.FromObject(View.Bounds.Width - 30));
            });

            var buyButtons = new UIButton[]
            {
                new UIButton(),
                new UIButton(),
                new UIButton(),
                new UIButton(),
                new UIButton()
            };
            createBuyItems(buyButtons, buyInfoView);
            buyButtons[0].TouchUpInside += (s, e) =>
            {
                logger.Log("点击任性看");
            };
            buyButtons[1].TouchUpInside += (s, e) =>
            {
                logger.Log("点击月度看");
            };
            buyButtons[2].TouchUpInside += (s, e) =>
            {
                logger.Log("点击季度会员");
            };
            buyButtons[3].TouchUpInside += (s, e) =>
            {
                logger.Log("点击年度会员");
            };
            buyButtons[4].TouchUpInside += (s, e) =>
            {
                logger.Log("点击至尊VIP会员");
            };
        }
        private void createBuyItems(UIButton[] buyButtons, UIView buyInfoView)
        {
            var infoStrings = new string[][]
            {
                new string[] { "Zhou.png", "任性看", "18元/周" },
                new string[] { "Yue.png", "月度看", "59元/月" },
                new string[] { "Jidu.png", "季度会员", "138元/季" },
                new string[] { "Nianduhuiyuan.png", "年度会员", "498元/年" },
                new string[] { "Vip.png", "至尊VIP会员", "人工服务，独享单台服务器，永不卡顿", "1588元/年" }
            };
            var oneHeight = 60;
            var oneViewWidth = View.Bounds.Width - 30;
            for (var i = 0; i < infoStrings.Length; i++)
            {
                int pixHeight = 0;
                if (i == (infoStrings.Length - 1))
                {
                    pixHeight = 20;
                }
                var top = i * (oneHeight + 10);
                var oneView = new UIView();
                oneView.SetParent(buyInfoView);
                oneView.Layer.CornerRadius = 5;
                oneView.ClipsToBounds = true;
                oneView.Layer.BorderWidth = 0.2f;
                oneView.Layer.BorderColor = new CGColor(0.5f, 0.5f, 0.5f);
                oneView.MakeConstraints(m =>
                {
                    m.Left.EqualTo(NSObject.FromObject(0));
                    m.Top.EqualTo(NSObject.FromObject(top));
                    m.Width.EqualTo(NSObject.FromObject(oneViewWidth));
                    m.Height.EqualTo(NSObject.FromObject(oneHeight + pixHeight));
                });
                var itemImageView = new UIImageView();
                var imgitemImageView = UIImage.FromFile($"{App.HotStorageRoot}/res/user/" + infoStrings[i][0]);
                itemImageView.Image = new UIImage(imgitemImageView.CGImage, 2, UIImageOrientation.Up);
                itemImageView.SetParent(oneView);
                itemImageView.MakeConstraints(m =>
                {
                    m.CenterY.EqualTo(oneView.CenterY());
                    m.Left.EqualTo(NSObject.FromObject(10));
                    m.Height.EqualTo(NSObject.FromObject(30));
                    m.Width.EqualTo(NSObject.FromObject(30));
                });

                var itemLabelView = new UIView();
                itemLabelView.SetParent(oneView);
                itemLabelView.MakeConstraints(m =>
                {
                    m.Left.EqualTo(NSObject.FromObject(50));
                    m.Top.EqualTo(oneView);
                    m.Height.EqualTo(NSObject.FromObject(40));
                    m.Width.EqualTo(NSObject.FromObject(oneViewWidth - 100));
                });
                var label1 = new UILabel();
                label1.SetParent(itemLabelView);
                label1.Text = infoStrings[i][1];
                label1.TextColor = UIColor.FromRGB(33, 33, 33);
                label1.MakeConstraints(m =>
                {
                    m.Top.EqualTo(NSObject.FromObject(10));
                    m.Left.EqualTo(NSObject.FromObject(0));
                    m.Height.EqualTo(NSObject.FromObject(20));
                    m.Width.EqualTo(itemLabelView);
                });
                var label2 = new UILabel();
                label2.SetParent(itemLabelView);
                label2.Text = infoStrings[i][2];
                label2.TextColor = UIColor.FromRGB(93, 93, 93);
                label2.Font = UIFont.SystemFontOfSize(14);
                label2.MakeConstraints(m =>
                {
                    m.Top.EqualTo(NSObject.FromObject(30));
                    m.Left.EqualTo(NSObject.FromObject(0));
                    m.Height.EqualTo(NSObject.FromObject(20));
                    m.Width.EqualTo(NSObject.FromObject(oneViewWidth - 100));
                });
                if (i == (infoStrings.Length - 1))
                {
                    var label3 = new UILabel();
                    label3.SetParent(itemLabelView);
                    label3.Text = infoStrings[i][3];
                    label3.TextColor = UIColor.FromRGB(93, 93, 93);
                    label3.Font = UIFont.SystemFontOfSize(14);
                    label3.MakeConstraints(m =>
                    {
                        m.Top.EqualTo(NSObject.FromObject(50));
                        m.Left.EqualTo(NSObject.FromObject(0));
                        m.Height.EqualTo(NSObject.FromObject(20));
                        m.Width.EqualTo(NSObject.FromObject(oneViewWidth - 100));
                    });
                }

                buyButtons[i].SetParent(oneView);
                buyButtons[i].MakeConstraints(m =>
                {
                    m.Right.EqualTo(NSObject.FromObject(0));
                    m.Top.EqualTo(NSObject.FromObject(0));
                    m.Bottom.EqualTo(NSObject.FromObject(0));
                    m.Width.EqualTo(NSObject.FromObject(60));
                });

                var itemArrowImageView = new UIImageView();
                var imgitemArrowImageView = UIImage.FromFile($"{App.HotStorageRoot}/res/user/Fanhui1.png");
                itemArrowImageView.Image = new UIImage(imgitemArrowImageView.CGImage, 2, UIImageOrientation.Up);
                itemArrowImageView.SetParent(buyButtons[i]);
                itemArrowImageView.MakeConstraints(m =>
                {
                    m.CenterY.EqualTo(buyButtons[i].CenterY());
                    m.Right.EqualTo(NSObject.FromObject(0));
                    m.Height.EqualTo(NSObject.FromObject(40));
                    m.Width.EqualTo(NSObject.FromObject(40));
                });
            }
        }
    }
}